package com.cheetah.mapper;

import com.cheetah.entity.Org;
import java.util.List;

import org.apache.ibatis.annotations.*;
import org.apache.ibatis.type.JdbcType;

@Mapper
public interface OrgMapper {
    @Delete({
        "delete from org",
        "where id = #{id,jdbcType=INTEGER}"
    })
    int deleteByPrimaryKey(Integer id);

    @Insert({
        "insert into org (name,`desc`,",
        "long_name,long_id,",
        "parent,network_id,",
        "weight)",
        "values (#{name,jdbcType=VARCHAR},#{desc,jdbcType=VARCHAR},",
        "#{longName,jdbcType=VARCHAR},#{longId,jdbcType=VARCHAR},",
        "#{parent,jdbcType=INTEGER},#{networkId,jdbcType=VARCHAR},",
        "#{weight,jdbcType=INTEGER})"
    })
    @SelectKey(statement="SELECT LAST_INSERT_ID()", keyProperty="id", before=false, resultType=Integer.class)
    int insert(Org record);

    @Select({
        "select",
        "*",
        "from org",
        "where id = #{id,jdbcType=INTEGER}"
    })
    @Results({
        @Result(column="id", property="id", jdbcType=JdbcType.INTEGER, id=true),
        @Result(column="name", property="name", jdbcType=JdbcType.VARCHAR),
        @Result(column="desc", property="desc", jdbcType=JdbcType.VARCHAR),
        @Result(column="long_name", property="longName", jdbcType=JdbcType.VARCHAR),
        @Result(column="long_id", property="longId", jdbcType=JdbcType.VARCHAR),
        @Result(column="parent", property="parent", jdbcType=JdbcType.INTEGER),
        @Result(column="network_id", property="networkId", jdbcType=JdbcType.VARCHAR),
        @Result(column="weight", property="weight", jdbcType=JdbcType.INTEGER)
    })
    Org selectByPrimaryKey(Integer id);

    @Select({
        "select",
        "id, name, desc, long_name, long_id, parent, network_id, weight",
        "from org"
    })
    @Results({
        @Result(column="id", property="id", jdbcType=JdbcType.INTEGER, id=true),
        @Result(column="name", property="name", jdbcType=JdbcType.VARCHAR),
        @Result(column="desc", property="desc", jdbcType=JdbcType.VARCHAR),
        @Result(column="long_name", property="longName", jdbcType=JdbcType.VARCHAR),
        @Result(column="long_id", property="longId", jdbcType=JdbcType.VARCHAR),
        @Result(column="parent", property="parent", jdbcType=JdbcType.INTEGER),
        @Result(column="network_id", property="networkId", jdbcType=JdbcType.VARCHAR),
        @Result(column="weight", property="weight", jdbcType=JdbcType.INTEGER)
    })
    List<Org> selectAll();

    @Update({
        "update org",
        "set name = #{name,jdbcType=VARCHAR},",
          "desc = #{desc,jdbcType=VARCHAR},",
          "long_name = #{longName,jdbcType=VARCHAR},",
          "long_id = #{longId,jdbcType=VARCHAR},",
          "parent = #{parent,jdbcType=INTEGER},",
          "network_id = #{networkId,jdbcType=VARCHAR},",
          "weight = #{weight,jdbcType=INTEGER}",
        "where id = #{id,jdbcType=INTEGER}"
    })
    int updateByPrimaryKey(Org record);

    @Select({
            "SELECT * FROM org where name = #{name,jdbcType=VARCHAR}",
                    "and parent = #{parent,jdbcType=INTEGER} "
    })
//    @Results({
//            @Result(column="id", property="id", jdbcType=JdbcType.INTEGER, id=true),
//            @Result(column="name", property="name", jdbcType=JdbcType.VARCHAR),
//            @Result(column="desc", property="desc", jdbcType=JdbcType.VARCHAR),
//            @Result(column="long_name", property="longName", jdbcType=JdbcType.VARCHAR),
//            @Result(column="long_id", property="longId", jdbcType=JdbcType.VARCHAR),
//            @Result(column="parent", property="parent", jdbcType=JdbcType.INTEGER),
//            @Result(column="network_id", property="networkId", jdbcType=JdbcType.VARCHAR),
//            @Result(column="weight", property="weight", jdbcType=JdbcType.INTEGER)
//    })
    Org selectByParentIdAndName(@Param("name") String name,@Param("parent") int parent);

    @Update({
            "update org o set o.long_name = replace(o.long_name,#{oldPart,jdbcType=VARCHAR},#{newPart,jdbcType=VARCHAR}),",
                    "o.update_time=NOW()",
                    "where o.long_id LIKE concat(#{longId,jdbcType=VARCHAR},'%')"
    })
    int updateSubOrgLongName(@Param("oldPart") String oldPart,
                             @Param("newPart") String newPart,
                             @Param("longId") String longId);

    @Update({
            "update org o set o.long_name = replace(o.long_name,#{oldPart,jdbcType=VARCHAR},#{newPart,jdbcType=VARCHAR}),",
            "o.long_id = replace(o.long_id,#{oldIdPart,jdbcType=VARCHAR},#{newIdPart,jdbcType=VARCHAR}),",
            "o.update_time=NOW()",
            "where o.long_id LIKE concat(#{longId,jdbcType=VARCHAR},'%')"
    })
    int updateSubOrgLongNameAndLongId(@Param("oldPart") String oldPart,
                                      @Param("newPart") String newPart,
                                      @Param("oldIdPart") String oldIdPart,
                                      @Param("newIdPart") String newIdPart,
                                      @Param("longId") String longId);

    @Update({
            "update org o set o.name = #{name,jdbcType=VARCHAR}",
            "where o.id=#{id,jdbcType=INTEGER}"
    })
    int updateName(@Param("id") Integer id,@Param("name") String name);

    @Update({
            "update org o set o.long_id = #{longId,jdbcType=VARCHAR}",
            "where o.id=#{id,jdbcType=INTEGER}"
    })
    int updateLongId(@Param("id") Integer id,@Param("longId") String longId);

    @Update({
            "update org o set o.parent = #{parent,jdbcType=INTEGER}",
            "where o.id=#{id,jdbcType=INTEGER}"
    })
    int updateParentId(@Param("id") Integer id,@Param("parent") Integer parent);

    @Update({
            "update org o set o.status = 0",
            "where o.id=#{id,jdbcType=INTEGER}"
    })
    int delete(@Param("id") Integer id);
}